//
//  MyPublicActionViewController.m
//  com.modada.new
//
//  Created by ferver7 on 2019/5/12.
//  Copyright © 2019 ferver7. All rights reserved.
//

#import "MyPublicActionViewController.h"
#import "ActionItemModel.h"
#import "ActionDetailViewController.h"
#import "ActionItemCell.h"

@interface MyPublicActionViewController ()
@property (weak, nonatomic) IBOutlet UITableView *selfTableView;
@property (nonatomic,strong) NSMutableArray *dataArr;
@property (nonatomic,assign) NSInteger page;
@end

@implementation MyPublicActionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    mNavitationTitle(@"我的发布");
    self.page = 1;
     self.dataArr = [[NSMutableArray alloc]init];
    self.selfTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadAllHuDongs];
    }];
    self.selfTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self loadAllHuDongs];
    }];
    [self loadAllHuDongs];
}
#pragma mark -
#pragma mark - **************** tableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActionItemCell * itemCell = mGetTableViewCell(@"actionItemCell");
    ActionItemModel * itemModel = self.dataArr[indexPath.row];
    [itemCell reloadViewForModel:itemModel];
    itemCell.actionBlock = ^(NSInteger index, UIButton *sender) {
        if (index == 1) {
            [self delListCellForModel:self.dataArr[indexPath.row]];
        }else{
            [self verbAction:self.dataArr[indexPath.row]];
        }
    };
    return itemCell;
}
-(void)delListCellForModel:(ActionItemModel *)model
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"摩嗒嗒提醒您" message:@"是否删除该条互动" preferredStyle: UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //点击按钮的响应事件；
        
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSMutableDictionary * infoDic = [[NSMutableDictionary alloc]init];
        setDickeyobj(infoDic,  @"1", @"add");
        setDickeyobj(infoDic,  model.ID, @"bbsid");
        [PPNetworkHelper POST:app_circle_deleteBbsInfo parameters:infoDic success:^(id responseObject) {
            if (YJPPSUCCESS(responseObject)) {
                [self.dataArr removeObject:model];
                
            }else{
                
            }
            [self.selfTableView reloadData];
        } failure:^(NSError *error) {
           
        } showLoading:self.view];
    }]];
    //弹出提示框；
    [self presentViewController:alert animated:true completion:nil];
}
-(void)verbAction:(ActionItemModel *)model
{
    NSMutableDictionary * infoDic = [[NSMutableDictionary alloc]init];
    setDickeyobj(infoDic,  @"1", @"add");
    setDickeyobj(infoDic,  model.ID, @"bbsid");
    if (model.state.integerValue == 1) {
        setDickeyobj(infoDic,  @"0", @"state");
    }else{
        setDickeyobj(infoDic,  @"1", @"state");
    }
    [PPNetworkHelper POST:app_circle_updateBbsState parameters:infoDic success:^(id responseObject) {
        if (YJPPSUCCESS(responseObject)) {
            if (model.state.integerValue == 1) {
                model.state = @"0";
            }else{
                model.state = @"1";
            }
        }else{
            
        }
         [self.selfTableView reloadData];
    } failure:^(NSError *error) {
        
    } showLoading:self.view];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [ActionItemCell cellForHeightFor:self.dataArr[indexPath.row]];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActionDetailViewController * advc = mGetStoryboardViewController(@"Main", @"actionDetailViewController");
    ActionItemModel * itemModel = self.dataArr[indexPath.row];
    advc.bbsId = itemModel.ID;
    advc.isSelf = YES;
    NavigationController_pushVC(advc);
}
#pragma mark -
#pragma mark - **************** 数据请求
-(void)loadAllHuDongs
{
    if ([self.selfTableView.mj_header isRefreshing]) {
        self.page = 1;
    }
    if ([self.selfTableView.mj_footer isRefreshing]) {
        self.page ++;
    }
    
    NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
    
    setDickeyobj(parameters, @"10", @"limit");
    NSString * pageStr = [NSString stringWithFormat:@"%ld",self.page];
    setDickeyobj(parameters, pageStr, @"page");
    [PPNetworkHelper POST:app_circle_getUserReleaseList parameters:parameters success:^(id responseObject) {
        [self.selfTableView.mj_header endRefreshing];
        [self.selfTableView.mj_footer endRefreshing];
        if (self.page == 1 && self.dataArr.count > 0) {
            [self.dataArr removeAllObjects];
        }
        if (YJPPSUCCESS(responseObject)) {
            for (NSDictionary * dataD in responseObject[@"data"][@"list"]) {
                ActionItemModel * item = [[ActionItemModel alloc]init];
                [item setValuesForKeysWithDictionary:dataD];
                item.ID = dataD[@"id"];
                [self.dataArr addObject:item];
            }
            
            if ([responseObject[@"data"][@"list"] count] < 10) {
                [self.selfTableView.mj_footer endRefreshingWithNoMoreData];
                
            }
            
        }else{
            
        }
        [self.selfTableView reloadData];
    } failure:^(NSError *error) {
        [self.selfTableView.mj_header endRefreshing];
        [self.selfTableView.mj_footer endRefreshing];
    } showLoading:nil];
}
@end
